﻿//********************************************************************************//
//创建日期：<创建日期，2016-09-16>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：增表
//********************************************************************************//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Platform.Http;
using Web.Class;
using System.Text;

namespace Web.Pages.QuShui.DiXiaS.YongHuGL.HuJiGL
{
    public partial class ZengBiao : BasePage
    {
        #region 属性

        /// <summary>
        /// gridView主键(用户信息ID)
        /// </summary>
        private int BIAOXXID
        {
            get
            {
                return Convert.ToInt32(ViewState["_BIAOXXID"]);
            }
            set
            {
                ViewState["_BIAOXXID"] = value;
            }
        }
        /// <summary>
        /// 用户号
        /// </summary>
        private string YHH
        {
            get
            {
                return ViewState["_YHH"] as string;
            }
            set
            {
                ViewState["YHH"] = value;
            }
        }
        /// <summary>
        /// 保存结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 用户信息BLL
        /// </summary>
        QS_YG_YONGHUXXBLL _QS_YG_YONGHUXXBLL = new QS_YG_YONGHUXXBLL();

        /// <summary>
        /// 深井信息BLL
        /// </summary>
        QS_YH_SHENJINGXXBBLL _SJXXBLL = new QS_YH_SHENJINGXXBBLL();

        /// <summary>
        /// 取水口bll
        /// </summary>
        QS_YH_QUSHUIKXXBBLL _QSKBLL = new QS_YH_QUSHUIKXXBBLL();

        /// <summary>
        /// 水表信息
        /// </summary>
        QS_YH_SHUIBIAOXXB _SBXX = new QS_YH_SHUIBIAOXXB();

        /// <summary>
        /// 水表信息bll
        /// </summary>
        QS_YH_SHUIBIAOXXBBLL _SBXXBLL = new QS_YH_SHUIBIAOXXBBLL();


        /// <summary>
        /// 日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();
        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _spHelper = new SpHelper();
        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                Display();
                LoadData();
                InitData();
            }
        }

       

        /// <summary>
        /// 放弃
        /// </summary>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.Status = GridViewStatus.Init;
            Display();
            txtYongHuH.ClientEnabled = true;
            txtYongHuH.Focus();
            
        }

        /// <summary>
        /// 修改
        /// </summary>
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            this.Status = GridViewStatus.Add;
            Display();
            if (txtYongHuH.Text.Trim()=="")
            {
                txtYongHuH.ClientEnabled = true;
                txtYongHuH.Focus();
            }
            else
            {
                txtYongHuH.ClientEnabled = false;
                txtShuiBiaoBH.Focus();
            }
           
        }

        /// <summary>
        /// 保存
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateData())
            {
                SaveData();
            }
        }

        /// <summary>
        /// 前台事件回调方法
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            string yhh = this.hdCollection.Get("yhh").ToString();

            if (e.Parameter == "setinfo")
            {
                string msg = "";

                if (yhh != "")
                {
                    DataTable dt = _QS_YG_YONGHUXXBLL.SelectQS_YG_YONGHUXXesDynamic(string.Format("I_JLZT=0 AND S_YONGHUH='{0}'", yhh), null);
                    DataTable dtsj = _SJXXBLL.SelectQS_YH_SHENJINGXXBsDynamic(string.Format("S_JINGBH in (SELECT S_JINGBIANH FROM QS_YH_YONGHUSJGXB WHERE S_YONGHUBH='{0}' AND I_JLZT=0) AND I_JLZT=0", yhh), null);
                    
                    if (dtsj.Rows.Count > 0)
                    {
                        msg = string.Join("|", dt.Rows[0]["S_YONGHUM"].ToString(), dtsj.Rows[0]["S_JINGBH"].ToString(), dtsj.Rows[0]["S_LAOBIANH"].ToString(), dtsj.Rows[0]["S_JINGDIZ"].ToString());
                    }
                    else {
                        msg = string.Join("|", dt.Rows[0]["S_YONGHUM"].ToString(), "", "", "");
                    }
                }
                e.Result = e.Parameter + "|0|" + msg;
            }
        }
        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            // txtYongHuH.Text = "";
            //txtYONGHUM.Text = "";
            //txtYJingBH.Text = "";
           // txtJingBH.Text = "";
            //txtQuShuiDD.Text = "";
            txtShuiBiaoBH.Text = "";
            txtBiaoHao.Text = "";
            txtBiaoJing.Text = "";
            if (BusinessTool.WType==2)
            {
                cmbCengHao.Text = "";
            }
            cmbShuiLiangLB.Text = "";
            cmbBiaoFenL.Text = "";
            BeiZhu.Text = "";
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            int wtype = BusinessTool.WType;
            if (wtype == 2)
            {
                Span1.InnerText = "井编号：";
                Span2.InnerText = "原井编号：";
            }
            else {
                Span1.InnerText = "取水口编号：";
                Span2.InnerText = "取水口名：";
            }
            //加载册号
            LoadCH();
            //加载水量类别
            LoadSLLB();
            //加载表分类
            LoadBFL();

        }

        /// <summary>
        /// 加载册号
        /// </summary>
        private void LoadCH()
        {
            int wtype = BusinessTool.WType;
            string[] arr1 = { "001", "002", "003", "005", "008", "009", "101", "106", "111", "113", "115", "204", "207", "208", "209", "210", "213", "214", "216", "217", "218" };
            string[] arr2 = { "600" };
            string[] arr = wtype == 1 ? arr2 : arr1;
            
            for (int i = 0; i < arr.Length; i++)
            {
                cmbCengHao.Items.Add(arr[i], arr[i]);
            }
            if (wtype == 1)
            {
                cmbCengHao.SelectedIndex = 0;
            }
        }

        /// <summary>
        /// 加载水量类别
        /// </summary>
        private void LoadSLLB()
        {
            //1	普通采
            //2	采灌采
            //3	采灌灌
            //4	矿泉采
            //5	专灌
            int wtype = BusinessTool.WType;
            if (wtype == 1)
            {
                cmbShuiLiangLB.ClientEnabled = false;
            }
            else
            {
                cmbShuiLiangLB.Items.Clear();
                cmbShuiLiangLB.ClientEnabled = true;
                cmbShuiLiangLB.Items.Add("普通采", "1");
                cmbShuiLiangLB.Items.Add("采灌采", "2");
                cmbShuiLiangLB.Items.Add("采灌灌", "3");
                cmbShuiLiangLB.Items.Add("矿泉采", "4");
                cmbShuiLiangLB.Items.Add("专灌", "5");
            }
        }

        /// <summary>
        /// 加载表分类
        /// </summary>
        private void LoadBFL()
        {
            int wtype = BusinessTool.WType;
            if (wtype == 2)
            {
                cmbBiaoFenL.Items.Add("总表", "001");
                cmbBiaoFenL.Items.Add("分表", "002");
                cmbBiaoFenL.Items.Add("虚拟表", "003");
            }
        }


        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            string yonghuh = "";
            if (txtYongHuH.Text.Trim()=="")
            {
                yonghuh = this.Request.QueryString["yonghuh"];
                this.YHH = yonghuh;
                txtYongHuH.Text = yonghuh;
                txtYongHuH.ClientEnabled = false;
            }
            else
            {
                yonghuh = txtYongHuH.Text.Trim();
            }
            if (yonghuh != "" && yonghuh != null)
            {
                
                DataTable dt = _QS_YG_YONGHUXXBLL.SelectQS_YG_YONGHUXXesDynamic(string.Format("I_JLZT=0 AND S_YONGHUH='{0}'", yonghuh), null);
                txtYONGHUM.Text = dt.Rows.Count > 0 ? dt.Rows[0]["S_YONGHUM"].ToString() : "";
                if (BusinessTool.WType == 2)
                {
                    DataTable dtsj = _SJXXBLL.SelectQS_YH_SHENJINGXXBsDynamic(string.Format("S_JINGBH in (SELECT S_JINGBIANH FROM QS_YH_YONGHUSJGXB WHERE S_YONGHUBH='{0}' AND I_JLZT=0) AND I_JLZT=0", yonghuh), null);
                    if (dtsj.Rows.Count < 1)
                    {
                        PageTools.AlertMsg(this, "该用户还有没有井编号，请先增井！");
                    }
                    else
                    {
                        txtJingBH.Items.Clear();
                        for (int i = 0; i < dtsj.Rows.Count; i++)
                        {
                            txtJingBH.Items.Add(dtsj.Rows[i]["S_JINGBH"].ToString());
                        }

                        txtJingBH.Text = dtsj.Rows[0]["S_JINGBH"].ToString();
                        txtYJingBH.Text = dtsj.Rows[0]["S_LAOBIANH"].ToString();
                        txtQuShuiDD.Text = dtsj.Rows[0]["S_JINGDIZ"].ToString();
                    }

                }
                else
                {
                    DataTable dtqsk = _QSKBLL.SelectQS_YH_QUSHUIKXXBsDynamic(string.Format("S_QUSHUIKBH in (SELECT S_QUSHUIKH FROM QS_YH_YONGHUQSKGX WHERE S_YONGHUH='{0}' AND I_JLZT=0) AND I_JLZT=0", yonghuh), null);
                    if (dtqsk.Rows.Count < 1)
                    {
                        PageTools.AlertMsg(this, "该用户还有没有取水口编号，请先增取水口！");
                    }
                    else
                    {
                        txtJingBH.Items.Clear();
                        for (int i = 0; i < dtqsk.Rows.Count; i++)
                        {
                            txtJingBH.Items.Add(dtqsk.Rows[i]["S_QUSHUIKBH"].ToString());
                        }
                        txtJingBH.Text = dtqsk.Rows[0]["S_QUSHUIKBH"].ToString();
                        txtYJingBH.Text = dtqsk.Rows[0]["S_QUSHUIKM"].ToString();
                        txtQuShuiDD.Text = dtqsk.Rows[0]["S_QUSHUIDZ"].ToString();
                    }
                }
                
            }
        }

        /// <summary>
        ///用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {
            switch (this.Status)
            {
                case GridViewStatus.Init:
                    btnEdit.Visible = true;
                    btnEdit.Enabled = true;
                    btnSave.Visible = false;
                    btnCancel.Visible = false;
                   
                    SetText(false);
                    break;
                case GridViewStatus.Update:
                    btnEdit.Visible = false;
                    btnSave.Visible = true;
                    btnCancel.Visible = true;
                    txtShuiBiaoBH.ClientEnabled = false;
                    SetText(true);
                    break;
                case GridViewStatus.Add:
                    btnEdit.Visible = false;
                    btnSave.Visible = true;
                    btnCancel.Visible = true;
                    InitControl();
                    SetText(true);
                    break;
            }
        }


        /// <summary>
        /// 设置输入框状态
        /// </summary>
        /// <param name="b"></param>
        private void SetText(bool b)
        {
            txtShuiBiaoBH.ClientEnabled = b;
            txtBiaoHao.ClientEnabled = b;
            txtBiaoJing.ClientEnabled = b;
            cmbCengHao.ClientEnabled = b;
            cmbShuiLiangLB.ClientEnabled = b;
            cmbBiaoFenL.ClientEnabled = b;
            BeiZhu.ClientEnabled = b;
            txtBiaoH2.ClientEnabled = b;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        private void SaveData()
        {
            int wtype = BusinessTool.WType;
            int slym = BusinessTool.GetSLYMorZWNY(1, wtype);
            int userid = LoginInfo.CurrentUser.UserID;
            OperateState operateState;
            //修改操作
            operateState = OperateState.Add;
            DataTable dtjh = _SBXXBLL.SelectQS_YH_SHUIBIAOXXBsDynamic(string.Format("i_jlzt=0 and S_SHUIBIAOBH='{0}'", txtShuiBiaoBH.Text), null);
             if (dtjh.Rows.Count > 0)
             {
                 PageTools.AlertMsg(this, "此水表号已经被使用，请重新填写！");
                 this.txtShuiBiaoBH.Focus();
             }
             else
             {
                 _SBXX.ID = 0;
                 _SBXX.I_JLZT = 0;
                 _SBXX.S_QUSHUIK = txtJingBH.Text;
                 _SBXX.S_SHUIBIAOBH = txtShuiBiaoBH.Text;
                 _SBXX.S_BIAOHAO = txtBiaoHao.Text;
                 _SBXX.S_CEHAO = cmbCengHao.Text != "" ? cmbCengHao.Value.ToString() : "";
                 _SBXX.S_SHUILIANGLB = cmbShuiLiangLB.Text != "" ? cmbShuiLiangLB.Value.ToString() : "";
                 _SBXX.S_BIAOFENL = cmbBiaoFenL.Text != "" ? cmbBiaoFenL.Value.ToString() : "";
                 _SBXX.S_BIAOBIAOJ = cmbBiaoBiaoJ.Text != "" ? cmbBiaoBiaoJ.Value.ToString() : "";
                 _SBXX.S_BEIZHU = BeiZhu.Text;
                 _SBXX.I_XUHAO = 1;
                 _SBXX.I_WTYPE = wtype;
                 _SBXX.S_KAISHINY = slym.ToString();
                 _SBXX.S_CAOZUOR = userid.ToString();
                 _SBXX.D_CAOZUORQ = DateTime.Now;
                 _SBXX.S_TIANJIAR = userid.ToString();
                 _SBXX.D_TIANJIARQ = DateTime.Now;
                 _SYS_LOGBLL.Log<QS_YH_SHUIBIAOXXB>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "用户管理-增表", operateState, _SBXX, null);
                 bool result = _SBXXBLL.InsertOrUpdate(_SBXX);
                 //用户水井关系表也要更新数据
                 string v = "";
                 _spHelper.P_QSYH_ZJORZB(wtype, 2, userid.ToString(), txtYongHuH.Text, txtShuiBiaoBH.Text, out v);
                 if (result && v == "1")
                 {
                     
                     PageTools.AlertMsg(this, "保存成功！");
                     this.Status = GridViewStatus.Init;
                     Display();
                     txtYongHuH.ClientEnabled = true;
                     txtYongHuH.Focus();
                 }
                 else
                 {
                     PageTools.AlertMsg(this, "保存失败！");
                 }
             }
        }

        /// <summary>
        /// 给输入框赋值
        /// </summary>
        private void SetData()
        {
            _SBXX = _SBXXBLL.SelectQS_YH_SHUIBIAOXXB(BIAOXXID);
            DataTable dtyh = _QS_YG_YONGHUXXBLL.SelectQS_YG_YONGHUXXesDynamic(string.Format("s_yonghuh =(select s_yonghuh from QS_YH_YONGHUSBGX where S_SHUIBIAOH='{0}' and i_jlzt=0) and i_jlzt=0", txtShuiBiaoBH.Text), null);
            txtYongHuH.Text = dtyh.Rows.Count > 0 ? dtyh.Rows[0]["S_YONGHUH"].ToString() : "";
            txtYONGHUM.Text = dtyh.Rows.Count > 0 ? dtyh.Rows[0]["S_YONGHUM"].ToString() : "";
            txtJingBH.Text = _SBXX.S_QUSHUIK;
            DataTable dt = _SJXXBLL.SelectQS_YH_SHENJINGXXBsDynamic(string.Format("s_jingbh='{0}' and i_jlzt=0", _SBXX.S_QUSHUIK), null);
            txtQuShuiDD.Text = dt.Rows.Count > 0 ? dt.Rows[0]["S_JINGDIZ"].ToString() : "";
            //txtYuanShuiBH.Text = _SBXX.S_YUANSHUIBH;
            txtBiaoHao.Text = _SBXX.S_BIAOHAO;
            txtBiaoJing.Text = "";
            cmbCengHao.Value = _SBXX.S_CEHAO;
            cmbShuiLiangLB.Value = _SBXX.S_SHUILIANGLB;
            cmbBiaoFenL.Value = _SBXX.S_BIAOFENL;
            cmbBiaoBiaoJ.Value = _SBXX.S_BIAOBIAOJ;
            txtZongBiaoH.Text = _SBXX.S_ZONGBIAOH;
            BeiZhu.Text = _SBXX.S_BEIZHU;
        }

        /// <summary>
        /// 保存验证
        /// </summary>
        /// <returns></returns>
        private bool ValidateData()
        {
            int wtype = BusinessTool.WType;
            if (this.txtJingBH.Text == "")
            {
                if (wtype == 2)
                {
                    PageTools.AlertMsg(this, "请先增井！");
                }
                else
                {
                    PageTools.AlertMsg(this, "请先增取水口！");
                }
                return false;
            }
            if (this.txtShuiBiaoBH.Text == "")
            {
                PageTools.AlertMsg(this, "水表编号不能为空！");
                this.txtShuiBiaoBH.Focus();
                return false;
            }
            if (this.txtShuiBiaoBH.Text.Length != 9 || !PageTools.ValidateString(this.txtShuiBiaoBH.Text, PageTools.eValidateType.数字))
            {
                PageTools.AlertMsg(this, "水表编号应为9位数字！");
                this.txtShuiBiaoBH.Focus();
                return false;
            }
            

            
            if (this.cmbCengHao.Text == "")
            {
                PageTools.AlertMsg(this, "册号不能为空！");
                this.cmbCengHao.Focus();
                return false;
            }
            if (wtype == 2)
            {
                if (this.txtBiaoHao.Text == "")
                {
                    PageTools.AlertMsg(this, "表号不能为空！");
                    this.txtBiaoHao.Focus();
                    return false;
                }
                if (this.cmbShuiLiangLB.Text == "")
                {
                    PageTools.AlertMsg(this, "水量类别不能为空！");
                    this.cmbShuiLiangLB.Focus();
                    return false;
                }
                if (this.cmbBiaoFenL.Text == "")
                {
                    PageTools.AlertMsg(this, "表分类不能为空！");
                    this.cmbBiaoFenL.Focus();
                    return false;
                }
            }

            

            return true;
        }

        #endregion

        protected void btnChaXun_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void txtJingBH_SelectedIndexChanged(object sender, EventArgs e)
        {
            int wtype = BusinessTool.WType;
            DataTable dt = null;
            if (wtype==2)
            {
                dt = _SJXXBLL.SelectQS_YH_SHENJINGXXBsDynamic(string.Format("S_JINGBH='{0}' and i_jlzt=0", txtJingBH.Text), null);
                if (dt.Rows.Count>0)
                {
                    txtYJingBH.Text = dt.Rows[0]["S_LAOBIANH"].ToString();
                    txtQuShuiDD.Text = dt.Rows[0]["S_JINGDIZ"].ToString();
                }
            }
            else
            {
                dt = _QSKBLL.SelectQS_YH_QUSHUIKXXBsDynamic(string.Format("S_QUSHUIKBH='{0}' and i_jlzt=0", txtJingBH.Text), null);
                if (dt.Rows.Count > 0)
                {
                    txtYJingBH.Text = dt.Rows[0]["S_QUSHUIKM"].ToString();
                    txtQuShuiDD.Text = dt.Rows[0]["S_QUSHUIDZ"].ToString();
                }
            }
           
        }
    }
}